/*
 * SaleBO.java
 *
 * Created on March 31, 2007, 4:20 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.jjenkins.data;
 
/**
 * BO for Sale
 * @author Rick
 */
public class SaleBO extends RevenueSourceBO{
    private String rs_id;
    private double quantity;
    private String prod_id;
    private ForSaleBO forSale = null;
    private String forSaleID;
    private TransactionLineBO line;
    String desc = "";
    double price = 0;//i put these 2 variables up here to lower database reads... if we read it once, then it will
    //still be in cache if we try and get it again.
    
    /** Creates a new instance of SaleBO */
    public SaleBO() {
    }
    
    /**
     *
     * @return
     */
    public String createDescription() {
        
        if(desc.isEmpty()){//dont know if .isempy will work...
            System.out.println("desc in saleBO is empty!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            try{
                setForSaleID(getForSale().getId());
                desc = ((PhysicalBO)PhysicalDAO.getInstance().read(getForSaleID())).getConceptual().getDescription();
            }catch (DataException e){
                e.printStackTrace();
            }
            setDescription(desc);
        }
        
        return desc;
    }
    
    /**
     *
     * @return
     */
    public double calcPrice() {
        if(price == 0){
            try {
                System.out.println("prod: "+prod_id+", rev:"+rs_id+", forsale:"+forSaleID);
                setForSaleID(getForSale().getId());
                price = quantity * SaleDAO.getInstance().getProductPrice(forSaleID);//this is returning a saleDAO..doing a read in SALEDAO
                this.setPrice(price);
            } catch (DataException ex) {
                ex.printStackTrace();
            }
        }
        
        return price;
    }
    
    /**
     *
     * @return
     */
    public String getRs_id() {
        return rs_id;
    }
    
    /**
     *
     * @param rs_id
     */
    public void setRs_id(String rs_id) {
        this.rs_id = rs_id;
        setDirty(true);
    }
    
    /**
     *
     * @return
     */
    public double getQuantity() {
        return quantity;
    }
    
    /**
     *
     * @param quantity
     */
    public void setQuantity(double quantity) {
        this.quantity = quantity;
        setDirty(true);
    }
    
    /**
     *
     * @return
     */
    public String getProd_id() {
        return prod_id;
    }
    
    /**
     *
     * @param prod_id
     */
    public void setProd_id(String prod_id) {
        this.prod_id = prod_id;
        setDirty(true);
    }
    
    /**
     *
     * @return
     */
    public ForSaleBO getForSale() {
        if(forSale == null){
            try {
                return (ForSaleBO)ForSaleDAO.getInstance().read(forSaleID);
            } catch (DataException ex) {
                ex.printStackTrace();
            }
        }
        return forSale;
    }
    
    /**
     *
     * @param forSale
     */
    public void setForSale(ForSaleBO forSale) {
        this.forSale = forSale;
        setForSaleID(forSale.getId());
        setDirty(true);
    }
    
    /**
     *
     * @return
     */
    public String getForSaleID() {
        return forSaleID;
    }
    
    /**
     *
     * @param forSaleID
     */
    public void setForSaleID(String forSaleID) {
        this.forSaleID = forSaleID;
        setDirty(true);
    }
    
    /**
     *
     * @return
     */
    public TransactionLineBO getLine() {
        return line;
    }
    
    /**
     *
     * @param line
     */
    public void setLine(TransactionLineBO line) {
        this.line = line;
        setDirty(true);
    }
    
}
